﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewStart
{
    public partial class CycleForm : Form
    {
        delegate void show();
        public static AppForm a;
        public static MenuForm m;

        public CycleForm()
        {
            SettingsHelper.GetSettings();
            a = new AppForm();
            m = new MenuForm();
            MouseHelper.Initialize(this);

            this.Opacity = 0;
            this.ShowInTaskbar = false;
            new Thread(new ThreadStart(Cycle)).Start();
        }

        void Cycle()
        {
            System.IO.Pipes.NamedPipeServerStream server = new System.IO.Pipes.NamedPipeServerStream("StartSignal");

            while (true)
            {
                server.WaitForConnection();
                byte[] buffer = new byte[1];
                server.Read(buffer, 0, 1);
                if (buffer[0] == 1)
                {
                    this.Invoke(new show(ShowMenu));
                    server.Disconnect();
                }
            }
        }

        public static void Stop()
        {
            SettingsHelper.SaveSettings();
            MouseHelper.Dispose();
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        public void ShowMenu()
        {
            m.ShowDialog();
            if (m.openAppForm)
            {
                m.openAppForm = false;
                a.ShowDialog();
                if (a.ret)
                {
                    a.ret = false;
                    ShowMenu();
                }
            }
        }
    }
}
